package com.study.web;

import com.study.bean.Person;
import com.study.config.RedisUtil;
import com.study.config.RedisUtils;
import com.study.r.R;
import com.study.r.Rmap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author study
 * @version 1.0
 * @date 2020/6/6 20:34
 */
@RestController
@RequestMapping("redis")
@Api(tags = "Redis 测试")
public class RedisController {

    private static final Logger logger = LoggerFactory.getLogger(RedisController.class);

    private Person person;

    // @Autowired 报错起不来
    @Autowired(required = false)
    RedisUtils redisUtils;
    @Autowired(required = false)
    RedisUtil redisUtil;

    @PostConstruct
    void init() {
        //TODO redis ping
        String ping = redisUtils.ping();
        System.out.println(ping);
        person = new Person(1, "测试", new Date());
        List<Person> arrayList = new ArrayList();
        arrayList.add(person);
        for (int i = 0; i < 10; i++) {
            person.setAge(i);
            arrayList.add(person);
        }
        if (redisUtils != null) {
            //TODO 取出来集合自己  转换 类型
            redisUtils.set("perList", arrayList);
            Object perList = redisUtils.get("perList");
            List<Person> list = (List<Person>) perList;
            for (Person p : list) {
                System.out.println(p);
            }
        }
    }


    @GetMapping("per")
    @ApiOperation("redis操作存储值")
    public Person person() {
        String ping = redisUtils.ping();
        System.out.println(ping);
        if (redisUtil != null) {
            logger.info("redis操作");
            Object o = redisUtil.get("spredis:per");
            Person personRedis = (Person) o;
            if (personRedis != null) {
                return personRedis;
            } else {
                person.setAge(person.getAge() + 1);
                redisUtil.set("spredis:per", person);
                return person;
            }
        }
        return person;
    }

    @GetMapping("localper")
    @ApiOperation("localswagger测试")
    public R<Person> personr() {
        Person personRedis = null;
        if (redisUtil != null) {
            logger.info("redis操作");
            Object o = redisUtil.get("spredis:per");
            personRedis = (Person) o;
            if (personRedis != null) {
            } else {
                person.setAge(person.getAge() + 1);
                redisUtil.set("spredis:per", person);
            }
        } else {
            personRedis = new Person().setAge(1).setBirthday(new Date()).setName("测试");
        }
        logger.info("result {}", personRedis.toString());
        R<Person> personR = new R<>();
        return personR.ok().result(personRedis).data("resultMap", personR);
    }

    @GetMapping("localpermap")
    @ApiOperation("localswagger测试Map")
    public R<Person> personrData() {
        Person personRedis = null;
        if (/*redisUtil != null*/false) {
            logger.info("redis操作");
            Object o = redisUtil.get("spredis:per");
            personRedis = (Person) o;
            if (personRedis != null) {
            } else {
                person.setAge(person.getAge() + 1);
                redisUtil.set("spredis:per", person);
            }
        } else {
            personRedis = new Person().setAge(1).setBirthday(new Date()).setName("测试");
        }
        logger.info("result {}", personRedis.toString());
        logger.info("swagger-ui测试");
        Map map = new HashMap();
        map.put("methos", "personrData");
        R<Person> personR = new R<>();
        return R.ok().result(personRedis)
                .data("name", "测试")
                .dataBean("dataBean", personRedis);
    }

    @GetMapping("localpermapRamp")
    @ApiOperation("localswagger测试Map|Ramp")
    public Rmap<Person> personrDataRmap() {
        Person personRedis = null;
        if (redisUtil != null) {
            logger.info("redis操作");
            Object o = redisUtil.get("spredis:per");
            personRedis = (Person) o;
            if (personRedis != null) {
            } else {
                person.setAge(person.getAge() + 1);
                redisUtil.set("spredis:per", person);
            }
        } else {
            personRedis = new Person().setAge(1).setBirthday(new Date()).setName("测试");
        }
        logger.info("result {}", personRedis.toString());
        logger.info("swagger-ui测试");
        Rmap rmap = Rmap.ok("成功").datas(personRedis);
        System.out.println(rmap);
        return rmap;
    }

    @GetMapping("localpermapList")
    @ApiOperation("localswagger测试MapList")
    public R<List<Person>> personrDataList() {
        ArrayList<Person> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Person().setAge(i).setName("测试" + 1).setBirthday(new Date()));

        }
        return R.ok().result(list)
                .data("name", "测试")
                .dataBean("dataBean", list);
    }

    @GetMapping("localpermapListRmap")
    @ApiOperation("localswagger测试MapList||Ramp")
    public Rmap<List<Person>> personrDataListRmap() {
        ArrayList<Person> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Person().setAge(i).setName("测试" + 1).setBirthday(new Date()));
        }
        Rmap datas = Rmap.ok().datas(list);
        System.out.println(datas);
        return datas;
    }

    @GetMapping("hash/{id}")
    public void hashRedis(@PathVariable String id) {
        Date birthday = new Date();
        Person person = new Person(Integer.parseInt(id), "hash" + id, birthday);
        redisUtil.hset("hash", id, person);
    }

    @PostMapping("hash")
    public Person gethashRedis(String id) {
        Person hash = redisUtils.hget("hash", id, Person.class);
        System.out.println(hash);
        return hash;
    }
}
